package com.lxmes.gs.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.lxmes.gs.domain.TaskProgress;
import com.lxmes.gs.service.ITaskProgressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxmes.gs.mapper.MyTaskMapper;
import com.lxmes.gs.domain.MyTask;
import com.lxmes.gs.service.IMyTaskService;

/**
 * 我的任务Service业务层处理
 * 
 * @author wdf
 * @date 2023-04-24
 */
@Service
public class MyTaskServiceImpl extends ServiceImpl<MyTaskMapper,MyTask> implements IMyTaskService{
    
    @Autowired
    private MyTaskMapper myTaskMapper;
    @Autowired
    protected Validator validator;
    @Autowired
    private ITaskProgressService taskProgressService;
    /**
     * 查询我的任务
     * 
     * @param id 我的任务主键
     * @return 我的任务
     */
    @Override
    public MyTask selectMyTaskById(Long id){
        return myTaskMapper.selectById(id);
    }

    /**
     * 查询我的任务列表
     * 
     * @param myTask 我的任务
     * @return 我的任务
     */
    @Override
    public List<MyTask> selectMyTaskList(MyTask myTask){
        return myTaskMapper.selectList(Wrappers.lambdaQuery(myTask));
    }

    /**
     * 新增我的任务
     * 
     * @param myTask 我的任务
     * @return 结果
     */
    @Override
    public int insertMyTask(MyTask myTask){
        return myTaskMapper.insert(myTask);
    }

    /**
     * 修改我的任务
     * 
     * @param myTask 我的任务
     * @return 结果
     */
    @Override
    public int updateMyTask(MyTask myTask){
        return myTaskMapper.updateById(myTask);
    }

    /**
     * 批量删除我的任务
     * 
     * @param ids 需要删除的我的任务主键
     * @return 结果
     */
    @Override
    public int deleteMyTaskByIds(Long[] ids){
        return myTaskMapper.deleteMyTaskByIds(ids);
    }

    /**
     * 删除我的任务信息
     * 
     * @param id 我的任务主键
     * @return 结果
     */
    @Override
    public int deleteMyTaskById(Long id){
        return myTaskMapper.deleteById(id);
    }
    @Override
    public String imporIteminfo(List<MyTask> myTaskList, boolean updateSupport) {
        if (StringUtils.isNull(myTaskList) || myTaskList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MyTask myTask : myTaskList)
        {
            try
            {
                BeanValidators.validateWithException(validator, myTask);
                this.insertMyTask(myTask);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int receiveMyTask(Long[] ids) {
        List<MyTask> myTasks = baseMapper.selectBatchIds(Arrays.asList(ids));
        myTasks.stream()
                .filter(i -> i.getReceiveTime() == null)
                .forEach(i->{
                    i.setReceiveTime(new Date());
                    this.updateMyTask(i);
                    TaskProgress taskProgress = BeanUtil.copyProperties(i, TaskProgress.class);
                    taskProgressService.insertTaskProgress(taskProgress);
                });
        return 1;
    }
}
